<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class TaskModel{ 
    protected $CI;
    private $pageSize = 10;
    public function __construct(){
        $this->CI =& get_instance();
        $this->CI->load->database();
    }
    

    /**
     * [checkCodeByAccount description]
     * @return [type] [description]
     */
    public function taskList($data){
        $column = "id,cover,user_id as userId,
                    name,close_date as closeDate,type,reward,
                    close_time as closeTime,platform,'' as nick,'' as head,
                    create_time as createTime";
        if(!empty($data['pageSize'])){
            $pageSize = $data['pageSize'];
        }
        else{
            $pageSize = $this->pageSize;
        }
        if(isset($data['selected']) && $data['selected']){
            //精选，规则：最新的10条(不管其他条件)
            $info = $this->CI->db->select($column)->order_by('id', 'DESC')->get_where('task', array('status' => 0), $pageSize, 0);
            // var_dump($this->CI->db->last_query());exit;
            $ret['status'] = 0;
            $result = $info->result();
            // var_dump($result);exit;
        }
        else{
            //非精选，过滤类型跟数额，瀑布式刷新         
            $fileterArray = array(
                'status' => 0
            );   
            //是否过滤类型
            if(isset($data['type']) && $data['type']){
                $fileterArray['type like'] = '%'.$data['type'] .'%';
            }
            //是否过滤金额
            if(isset($data['min']) && $data['min']){
                $fileterArray['reward>='] = $data['min'];
                // array_push($fileterArray,);
            }
            if(isset($data['max']) && $data['max']){
                $fileterArray['reward<='] = $data['max'];
            }
            if(isset($data['id']) &&$data['id']){
                $fileterArray['id<'] = $data['id'];
            }
            // var_dump($fileterArray);exit;
            
            $query = $this->CI->db->select($column)->order_by('id', 'DESC')->get_where('task', $fileterArray, $pageSize, 0);
            $ret['status'] = 0;
            $result = $query->result(); 
        }
        //查询发布人信息
        if(count($result)){
            $userArr = array();
            foreach ($result as $key => $value) {
                if(!in_array($value->userId,$userArr)){
                    array_push($userArr,$value->userId);
                }
            }        
            if(count($userArr)){
                $userStr = implode(",",$userArr);
                $extraInfo = $this->CI->db->query("SELECT nickname ,avatar,user_id FROM user_extra where user_id in (".$userStr.")");
                // var_dump($extraInfo->result());exit;
                foreach ($result as $key => $value) {
                    foreach ($extraInfo->result() as $extraKey => $extarValue) {
                        if($value->userId==$extarValue->user_id){
                            $result[$key]->nick = $extarValue->nickname;
                            $result[$key]->head = $extarValue->avatar;
                            break;
                        }
                    }
                }
            }
            
        }


        $ret['data'] = $result;
        
        return $ret;
    }



    /**
     * [taskDetail description]
     * @return [type] [description]
     */
    public function taskDetail($id,$keyword=0){
        $column = "id,cover,user_id as userId,status,notes,instruction,
                    name,close_date as closeDate,type,reward,
                    close_time as closeTime,platform,'' as nick,'' as head,watch_count as watchCount,
                    create_time as createTime";
        if($keyword){
            $column = $column . ',keyword';
        }
            
        $query = $this->CI->db->select($column)->get_where('task', array("id"=>$id),1, 0);
        $result = $query->result(); 
        // var_dump($result);exit;
        //查询发布人信息
        if(count($result)){
            $userId = $result[0]->userId;
            $extraInfo = $this->CI->db->query("SELECT nickname ,avatar,user_id FROM user_extra where user_id =$userId");
            $row = $extraInfo->row();
            if(isset($row->nickname)){
                $result[0]->nick = $row->nickname;
                $result[0]->head = $row->avatar;
            }
            //2更新浏览量
            $this->CI->db->set("watch_count",++$result[0]->watchCount)->where("id",$id)->update('task');
        }
        $ret['out_data'] = isset($result[0])?$result[0]:array();
        
        return $ret;
    }   

    /**
     * [cancelParma description]
     * @return [type] [description]
     */
    public function cancelParma($id){
        //1是否有过提交
        $query = "SELECT count(log.id) as total 
            from task_obtain as obtain
            join task_obtain_log as log on obtain.id = log.task_obtain_id
            where obtain.task_id = $id and log.status in(1,2)
            ";
        $info = $this->CI->db->query($query);
        $data = $info->row();
        if(isset($data->total)&&$data->total){
            $ret['submit'] = $data->total;
        }
        else{
            $ret['submit'] = 0;
        } 
        return $ret;
    }


    /**
     * [calcDeduction description]
     * @param  [type] $submitCount [description]
     * @param  [type] $closeDate   [description]
     * @param  [type] $closeTime   [description]
     * @return [type]              [description]
     */
    public function calcDeduction($userId,$taskId,$submitCount,$closeDate,$closeTime){
        $dateNow = date("Y-m-d H:i:s");
        if($closeTime && strtotime($closeTime)){
            //如果具体到了截止时间，且截止时间有效
            $deadline = $closeDate . ' ' . $closeTime;
        }
        else{
            //仅仅截止到了截止日期，默认为当天最后一秒
            $deadline = $closeDate . ' 23:59:59' ;
        }
        //查询保证金
        // echo $taskId
        $obtainDetail = $this->taskObtainDetail($taskId,$userId);
        if($obtainDetail){
            $reward = $obtainDetail->deposit;
        }
        else{
            $reward = 0;
        }
        // echo $dateNow,$deadline;exit;
        if($dateNow>=$deadline){
            //超时
            if($submitCount){
                //有过提交，扣除0.8
                $ret['deduction'] = floor($reward*0.8);
            }
            else{
                //未有提交，全部扣除
                $ret['deduction'] = $reward; 
            }
        }
        else{
            //未超时
            $sub = (strtotime($deadline)-strtotime($dateNow))/3600;
            if($sub>=48){
                //
                $ret['deduction'] = floor($reward*0.1);
            }
            else if($sub<48&&$sub>=24){
                $ret['deduction'] = floor($reward*0.2);

            }
            else if($sub<24&&$sub>=12){
                $ret['deduction'] = floor($reward*0.4);

            }
            else{
                $ret['deduction'] = floor($reward*0.6);

            }
        }
        $ret['undeduction'] = $reward - $ret['deduction'];
        return $ret;

    }
    

    /**
     * [overtimeStatus description]
     * @param  [type] $submitCount [description]
     * @param  [type] $closeDate   [description]
     * @param  [type] $closeTime   [description]
     * @return [type]              [description]
     */
    public function overtimeStatus($closeDate,$closeTime){
        $dateNow = date("Y-m-d H:i:s");
        if($closeTime && strtotime($closeTime)){
            //如果具体到了截止时间，且截止时间有效
            $deadline = $closeDate . ' ' . $closeTime;
        }
        else{
            //仅仅截止到了截止日期，默认为当天最后一秒
            $deadline = $closeDate . ' 23:59:59' ;
        } 
        if($dateNow>=$deadline){
            return 1; //超时
        }
        else{
            return 0;//未超时
        }

    }
    
    /**
     * [taskObtainDetail description]
     * @return [type] [description]
     */
    public function taskObtainDetail($taskId,$userId){
        $query = $this->CI->db->select("*")->get_where('task_obtain', array("user_id"=>$userId,"task_id"=>$taskId),1, 0);
        $result = $query->result(); 
        if(isset($result[0])){
            return $result[0];
        }
        else{
            return '';
        }
    }    
 /**
     * [taskObtainDetail description]
     * @return [type] [description]
     */
    public function taskObtainDetailById($Id,$userId){
        $query = $this->CI->db->select("*")->get_where('task_obtain', array("user_id"=>$userId,"id"=>$Id),1, 0);
        $result = $query->result(); 
        if(isset($result[0])){
            return $result[0];
        }
        else{
            return '';
        }
    }    

    /**
     * [taskObtainDetail description]
     * @return [type] [description]
     */
    public function taskObtainLog($taskId,$userId,$status){
         $query = $this->CI->db->select('count(log.id) as totalCount')
                ->join("task_obtain_log as log",'obtain.id = log.task_obtain_id','left')
                ->get_where('task_obtain as obtain', ["obtain.user_id"=>$userId,"obtain.task_id"=>$taskId,"obtain.status"=>$status],1,0);
        $result = $query->row(); 
        log_message("info","taskObtainLog,result:".json_encode($result));
        if(isset($result->totalCount)){
            return $result->totalCount;
        }
        else{
            return 0;
        }

    }  

    /**
     * [myObtainTask description]
     * @return [type] [description]
     */
    public function myObtainTask($userId,$data){   
        $column = "task_obtain.id,task_obtain.deposit,task_obtain.status as obtainStatus
                ,task.name,task.cover,task.status
                ,task.close_date as closeDate
                ,task.close_time as closeTime";
        //排序
        if(isset($data['id']) &&$data['id']){
            $fileterArray['task_obtain.id<'] = $data['id'];
        }
        $fileterArray['task.status>'] = 0;
        //是否过滤类型
        if(isset($data['status']) && $data['status']!=''){
            if($data['status']>0){//已经进行到下一步
                $fileterArray['task_obtain.status'] = $data['status'];
            }
            else if($data['status']==0){//所有未完成的
                $fileterArray['task.status'] =1; //当前任务处于进行中
            }
            else{
            }
        }
        if(!empty($data['pageSize'])){
            $pageSize = $data['pageSize'];
        }
        else{
            $pageSize = $this->pageSize;
        }
        // var_dump($fileterArray);exit;
        $fileterArray['task_obtain.user_id'] = $userId;
        $query = $this->CI->db->select($column)
        ->join("task","task_obtain.task_id = task.id")
        ->order_by('task_obtain.id', 'DESC')->get_where('task_obtain', $fileterArray, $pageSize, 0);
        $result = $query->result(); 
         
        return $result;
    }  

    /**
     * [updateTask description]
     * @return [type] [description]
     */
    public function updateObtainTask($obtainTaskId,$status,$reqData=''){
        $this->CI->db->trans_start();
        if($status==1){
            // 正常提交
            //1更改task_obtain表
            $status = $this->CI->db->set("status",1)->where("id",$obtainTaskId)->update('task_obtain');
            if(!$status){
                $this->CI->db->trans_rollback();
                return 2002;
            }
            //2添加记录
            $temp = array(
                "task_obtain_id"=>$obtainTaskId,
                "status"=>1,
            );
            $ret = $this->CI->db->insert('task_obtain_log', $temp);
            if(!$ret){
                $this->CI->db->trans_rollback();
                return 2002;
            }
        }
        else if($status==3){
            //延期
            //1计算要扣除的保证金
            if($reqData){

                $obtainDetail = $this->taskObtainDetail($reqData['taskId'],$reqData['userId']);
                if($obtainDetail){
                    $reward = $obtainDetail->deposit;
                }
                else{
                    $reward = 0;
                }

                // 1.1
                $overtime = $this->overtimeStatus($reqData['closeDate'],$reqData['closeTime']);
                if($overtime) {
                    $deduction = 0.1*$reward;
                }
                else{
                    $deduction = 0;
                }

            }
            else{
                $deduction = 0;
            }

            //2申请延期
            $cancelData['status'] = 3;
            $status = $this->CI->db->set($cancelData)->where("id",$obtainTaskId)->update('task_obtain');
            if(!$status){
                $this->CI->db->trans_rollback();
                return 2002;
            }
            //3添加记录
            $temp = array(
                "task_obtain_id"=>$obtainTaskId,
                "status"=>3,
                "instruction"=>isset($reqData['instruction'])?$reqData['instruction']:"",
                "deduction" => $deduction,
                "close_date" => $reqData['delaycloseDate'],//延期后时间
                "close_time" => $reqData['delaycloseTime'],//延期后时间
            );
            $ret = $this->CI->db->insert('task_obtain_log', $temp);
            if(!$ret){
                $this->CI->db->trans_rollback();
                return 2002;
            }
        }
        else if($status==6){
            //1计算要扣除的保证金
            if($reqData){
                // 1.1
                $cancelParma = $this->cancelParma($reqData['id']);
                // 1.2计算扣除金额，扣押金
                $dedu = $this->calcDeduction($reqData['userId'],$reqData['id'],$cancelParma['submit'],$reqData['closeDate'],$reqData['closeTime']);
                $deduction = $dedu['deduction'];

            }
            else{
                $deduction = 0;
            }

            //2申请取消
            $cancelData['status'] = 6;
            $status = $this->CI->db->set($cancelData)->where("id",$obtainTaskId)->update('task_obtain');
            if(!$status){
                $this->CI->db->trans_rollback();
                return 2002;
            }
            //3添加记录
            $temp = array(
                "task_obtain_id"=>$obtainTaskId,
                "status"=>6,
                "instruction"=>isset($reqData['instruction'])?$reqData['instruction']:"",
                "deduction" => $deduction
            );
            $ret = $this->CI->db->insert('task_obtain_log', $temp);
            if(!$ret){
                $this->CI->db->trans_rollback();
                return 2002;
            }
        }
        $this->CI->db->trans_complete();

        return 0;
    }


   /**
     * [adminTaskObtainList description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function adminTaskObtainList($data){
        $column = "
            task.id as taskId,task.name,task.keyword,task.user_id as taskUserId,task.type
            ,extra1.nickname as obtainNick,extra2.nickname as taskNick,obtain.id,task.status as taskStatus
            ,obtain.id as obtainId,obtain.user_id as obtainUserId
            ,obtain.status as obtainStatus,obtain.create_time as createTime,update_time as updateTime
            ";
        $totalColumn = "count(task.id) as totalcount";
        // var_dump($data);exit;
        if(empty($data['status'])){
        // 展示1(所有正在进行的任务)
            $fileterArray = array(
                'task.status=' => 1,
            );   
        }
        else{
        // 展示2(所有已完成的任务)
            $fileterArray = array(
                'task.status=' => 2,
            );   
        }
        // var_dump($fileterArray);exit;
        //过滤关键字
        if(isset($data['keyword']) && $data['keyword']){
            $fileterArray['task.keyword like'] = '%'.$data['keyword'] .'%';
        }
        
        //过滤状态
        if(isset($data['obtainStatus']) && $data['obtainStatus']>=0 &&$data['obtainStatus']!=''){
            $fileterArray['obtain.status'] = $data['obtainStatus'];
        }        

        $start = ($data['page']-1)>=0?($data['page']-1)*$data['size']:0;
        $query = $this->CI->db->select($column)
                ->join("task_obtain as obtain",'obtain.task_id = task.id','left')
                ->join("user_extra as extra1",'obtain.user_id = extra1.user_id')
                ->join("user_extra as extra2",'task.user_id = extra2.user_id')
                ->order_by('obtain.id', 'asc')
                ->get_where('task as task', $fileterArray, $data['size'], $start);
        $result = $query->result(); 
        // $obtainUserId = $taskUserId = '-1';
        // foreach ($result as $key => $value) {
        //     //查询用户
        //     $obtainUserId .= ','.$value->obtainUserId;
        //     $taskUserId .= ','.$value->taskUserId;
        // }
        // extraInfoBatch
        $totalQuery = $this->CI->db->select($totalColumn)
                ->join("task as task",'obtain.task_id = task.id')
                ->get_where('task_obtain as obtain', $fileterArray, 1, 0);
        $totalResult = $totalQuery->row();
        $ret['data'] = $result;
        $ret['out_data']['totalCount'] = isset($totalResult->totalcount)?$totalResult->totalcount:0;
        $ret['status'] = 0;
        return $ret;
    }   


    /**
     * [adminTaskDetail description]
     * @param  [type] $id [description]
     * @return [type]       [description]
     */
    public function adminTaskDetail($taskId){
        $column = "task.id as taskId,task.name,task.keyword,task.user_id as taskUserId,task.type
            ,task.status as taskStatus,task.close_date as closeDate,task.close_time as closeTime
            ,task.reward,task.cover,task.instruction,task.notes,task.number_requirement as numberRequirement
            ,obtain.id as obtainId,obtain.user_id as obtainUserId
            ,obtain.status as obtainStatus,obtain.create_time as createTime";
            
        $query = $this->CI->db->select($column)
                ->join("task_obtain as obtain",'obtain.task_id = task.id','left')
                ->get_where('task as task', ['task.id'=>$taskId]);
        $result = $query->row(); 
        $ret['out_data'] = array();
        if($result){
            $this->CI->load->model("userModel");
            $obtainInfo = $this->CI->userModel->mixUserInfo($result->obtainUserId);
            if($obtainInfo){
                $result->obtainAccount= $obtainInfo->account;
                $result->obtainNick= $obtainInfo->nickname;
                $result->obtainEmail= $obtainInfo->email;
            }
            $taskInfo = $this->CI->userModel->mixUserInfo($result->taskUserId);
            if($taskInfo){
                $result->taskAccount= $taskInfo->account;
                $result->taskNick= $taskInfo->nickname;
                $result->taskEmail= $taskInfo->email;
            }
            
            $ret['out_data'] = $result;
        }
        $ret['status'] = 0;
        return $ret;
    }  

    /**
     * [adminTaskDetail description]
     * @param  [type] $id [description]
     * @return [type]       [description]
     */
    public function adminTaskObtainDetail($taskObtainId){
        $column = "task.id as taskId,task.name,task.keyword,task.user_id as taskUserId,task.type
            ,task.status as taskStatus,task.close_date as closeDate,task.close_time as closeTime
            ,task.reward,task.cover,task.instruction,task.notes,task.number_requirement as numberRequirement
            ,obtain.id as obtainId,obtain.user_id as obtainUserId
            ,obtain.status as obtainStatus,obtain.create_time as createTime";
            
        $query = $this->CI->db->select($column)
                ->join("task as task",'obtain.task_id = task.id','left')
                ->get_where('task_obtain as obtain', ['obtain.id'=>$taskObtainId]);
        $result = $query->row(); 
        $ret['out_data'] = array();
        if($result){
            $this->CI->load->model("userModel");
            $obtainInfo = $this->CI->userModel->mixUserInfo($result->obtainUserId);
            if($obtainInfo){
                $result->obtainAccount= $obtainInfo->account;
                $result->obtainNick= $obtainInfo->nickname;
                $result->obtainEmail= $obtainInfo->email;
            }
            $taskInfo = $this->CI->userModel->mixUserInfo($result->taskUserId);
            if($taskInfo){
                $result->taskAccount= $taskInfo->account;
                $result->taskNick= $taskInfo->nickname;
                $result->taskEmail= $taskInfo->email;
            }
            
            $ret['out_data'] = $result;
        }
        $ret['status'] = 0;
        return $ret;
    }  


    /**
     * [adminTaskObtainStatistic description]
     * @param  [type] $status [description]
     * @return [type]       [description]
     */
    public function adminTaskObtainStatistic($status=0){
        $fileterArray = array();
        $fileterArray['status'] = $status;
        $totalQuery = $this->CI->db->select("count(id) as totalcount")
                ->get_where('task_obtain', $fileterArray, 1, 0);
        $totalResult = $totalQuery->row();
        return isset($totalResult->totalcount)?$totalResult->totalcount:0;
    }    

    /**
     * [adminTaskStatisticFound 按月统计支出、消费]
     * @param  [type] $status [description]
     * @return [type]       [description]
     */
    public function adminTaskStatisticFound($data){
        log_message('info', 'AdminStatisc/adminTaskStatisticFound,data:'.json_encode($data));     
        $fileterArray['status'] = 1;//支付成功
        if($data['syearmonth'])
            $fileterArray['create_month>='] = $data['syearmonth'] . ' 00:00:00';
        if($data['eyearmonth'])
            $fileterArray['create_month<='] = $data['eyearmonth'] . ' 23:59:59';
        log_message('info', 'AdminStatisc/adminTaskStatisticFound,fileterArray:'.json_encode($fileterArray));   
        // var_dump($fileterArray)
        //1查询收入，按月统计
        $income = $this->CI->db->select("
            create_month as createMonth
            ,sum(order_amount) as totalIncomeAmount
            ,count(id) as totalTaskCount
            ,0 as totalPayAmount")
            ->order_by("create_month",'asc')
            ->group_by("create_month")
            ->get_where('user_order', $fileterArray);
        log_message('info', 'AdminStatisc/adminTaskStatisticFound,last_query:'.$this->CI->db->last_query());   

        $incomeResult = $income->result();
        // var_dump($incomeResult);
        //2查询支出
        $pay = $this->CI->db->select("
            create_month as createMonth
            ,0 as totalIncomeAmount
            ,sum(order_amount) as totalPayAmount
            ,0 as totalTaskCount")
            ->order_by("create_month",'asc')
            ->group_by("create_month")
            ->get_where('user_reward_order', $fileterArray);
        $payResult = $pay->result();
        // var_dump($payResult);exit;
        foreach ($incomeResult as $key1 => $value1) {
            foreach ($payResult as $key2 => $value2) {
                if($value1->createMonth==$value2->createMonth){
                    $incomeResult[$key1]->totalPayAmount = $value2->totalPayAmount;
                    unset($payResult[$key2]);
                    break;
                }
            }
        }
        if(count($payResult)){
            $incomeResult = array_merge($incomeResult,$payResult);
            //排序
            usort($incomeResult,'TaskModel::usortResult');
        }
        return $incomeResult;
    }  
    
    /**
     * [adminTaskStatisticFoundList 按月统计列表，只查询最新的10条]
     * @param  [type] $status [description]
     * @return [type]       [description]
     */
    public function adminTaskStatisticFoundList($data,$type){
        $fileterArray['status'] = 1;//支付成功
        // if($data['syearmonth'])
        //     $fileterArray['syearmonth'] = $data['syearmonth'] . '00:00:00';
        // if($data['eyearmonth'])
        //     $fileterArray['eyearmonth'] = $data['eyearmonth'] . '23:59:59';
        if($type==1){

            //1查询收入，按月统计
            $income = $this->CI->db->select("
                order_number as orderNumber
                ,order_type as orderType
                ,create_time as createTime
                ,order_amount as orderAmount")
                ->order_by("create_month",'desc')
                ->get_where('user_order', $fileterArray,10,0);
            $Result = $income->result();
        }
        else{

            // var_dump($incomeResult);
            //2查询支出
            $pay = $this->CI->db->select("
                order_number as orderNumber
                ,order_type as orderType
                ,create_time as createTime
                ,order_amount as orderAmount")
                ->order_by("create_month",'desc')
                ->get_where('user_reward_order', $fileterArray,10,0);
            $Result = $pay->result();
        } 
        return $Result;
    }  

    public static  function usortResult($a,$b){
        if($a->createMonth>$b->createMonth)
            return 1;
        else 
            return -1;
    }
    
    /**
     * [adminTaskStatistic description]
     * @param  [type] $status [description]
     * @return [type]       [description]
     */
    public function adminTaskStatistic($status=''){
        $fileterArray = array();
        if($status===''){
            //全部
        }
        else{
            $fileterArray['status'] = $status;
        }
        $totalQuery = $this->CI->db->select("count(id) as totalcount")
                ->get_where('task', $fileterArray, 1, 0);
        $totalResult = $totalQuery->row();
        return isset($totalResult->totalcount)?$totalResult->totalcount:0;
    }  

    /**
     * [admintaskApplyDetail]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function admintaskApplyDetail($data){
        $obtainId = $data["obtainId"];
        $obtainStatus = $data["obtainStatus"];
        if(!($obtainStatus==3 || $obtainStatus==6)){
            $ret['status'] = 3010;
            return $ret;
        }
        $query = $this->CI->db
        ->select("id,task_obtain_id,instruction,close_date ,close_time")
        ->get_where('task_obtain_log', array('task_obtain_id ' => $obtainId,"status"=>$obtainStatus));
        $result = $query->row();
        // echo $this->CI->db->last_query();exit;
        $ret['status'] = 0;
        $ret['out_data'] = $result;
        return $ret;
        
    }

    /**
     * [taskObtainAwardDetail description]
     * @return [type] [description]
     */
    public function taskObtainAwardDetail($taskObtainId){
        //1查询用户信息
        $queryObtain = $this->CI->db->select("id,user_id,task_id,deposit ,student_award,left_deposit")
            ->get_where('task_obtain', array('id ' => $taskObtainId));
        $resultObtain = $queryObtain->row();
        if(!$resultObtain){
            $ret['status'] = 3002;
            return $ret;
        }
        $ret['out_data']['deposit'] = $resultObtain->deposit;
        $ret['out_data']['leftDeposit'] = $resultObtain->left_deposit;
        $ret['out_data']['studentAward'] = $resultObtain->student_award;
        $ret['out_data']['taskId'] = $resultObtain->task_id;
        //2任务奖励
        $queryTask = $this->CI->db->select("id,reward,name,keyword")
            ->get_where('task', array('id ' => $resultObtain->task_id));
        $resultTask = $queryTask->row();
        if(!$resultTask){
            $ret['status'] = 3000;
            return $ret;
        }
        $ret['out_data']['taskReward'] = $resultTask->reward;
        $ret['out_data']['taskName'] = $resultTask->name;
        $ret['out_data']['taskKeyword'] = $resultTask->keyword;
        
        //3是否是别人邀请来的
        $queryBase = $this->CI->db->select("id,beinvited_code,invite_code")
            ->get_where('user_extra', array('user_id' => $resultObtain->user_id));
        $resultBase = $queryBase->row();
        if(!$resultBase){
            $ret['out_data']['selfAccount'] = 0;
            $ret['out_data']['inviteAccount'] = 0;
            $ret['out_data']['inviteRewardList'] = array();
        }
        else{
            $ret['out_data']['selfAccount'] = $resultBase->invite_code;
            $ret['out_data']['inviteAccount'] = $resultBase->beinvited_code;
            //3是否领过邀请的奖励，如果领过，展示领过的对应任务
            $queryOrder = $this->CI->db->select("reward.id,reward.order_amount as orderAmount,release_id as taskId")
                ->join("user_order order"," reward.release_order_id=order.id and order.status=1","left")
                ->get_where('user_reward_order reward', array('reward.user_id' => $resultObtain->user_id,'reward.order_type'=>4,'reward.status'=>1));
            $resultOrder = $queryOrder->result();
            $ret['out_data']['inviteRewardList'] = $resultOrder;
        }
        $ret['status'] = 0;
        return $ret;
    }

}